package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolFixedAsset;
import cn.da.shuai.cool.finance.model.vo.SearchFixedAssetVo;
import cn.da.shuai.cool.finance.service.ICoolFixedAssetService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 固定资产表 前端控制器
 * </p>
 *
 * @author 大帅哥
 * @since 2023-05-01
 */
@Tag(name = "固定资产管理")
@RestController
@RequestMapping("/finance/fixed-asset")
@RequiredArgsConstructor
public class CoolFixedAssetController {

    private final ICoolFixedAssetService fixedAssetService;

    @Operation(summary = "分页查询固定资产")
    @PostMapping("/page")
    public R<Page<CoolFixedAsset>> page(@RequestBody SearchFixedAssetVo vo) {
        LambdaQueryWrapper<CoolFixedAsset> wrapper = new LambdaQueryWrapper<CoolFixedAsset>()
                .eq(vo.getAssetCode() != null, CoolFixedAsset::getAssetCode, vo.getAssetCode())
                .like(vo.getAssetName() != null, CoolFixedAsset::getAssetName, vo.getAssetName())
                .eq(vo.getAssetType() != null, CoolFixedAsset::getAssetType, vo.getAssetType())
                .ge(vo.getPurchaseStartDate() != null, CoolFixedAsset::getPurchaseDate, vo.getPurchaseStartDate())
                .le(vo.getPurchaseEndDate() != null, CoolFixedAsset::getPurchaseDate, vo.getPurchaseEndDate())
                .eq(vo.getDepartmentId() != null, CoolFixedAsset::getDepartmentId, vo.getDepartmentId())
                .eq(vo.getStatus() != null, CoolFixedAsset::getStatus, vo.getStatus())
                .orderByDesc(CoolFixedAsset::getPurchaseDate);

        return R.ok(fixedAssetService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper));
    }

    @Operation(summary = "获取固定资产详情")
    @GetMapping("/{id}")
    public R<CoolFixedAsset> getById(@PathVariable Long id) {
        return R.ok(fixedAssetService.getById(id));
    }

    @Operation(summary = "新增固定资产")
    @PostMapping
    public R<Boolean> add(@RequestBody CoolFixedAsset fixedAsset) {
        return R.ok(fixedAssetService.save(fixedAsset));
    }

    @Operation(summary = "修改固定资产")
    @PutMapping
    public R<Boolean> update(@RequestBody CoolFixedAsset fixedAsset) {
        return R.ok(fixedAssetService.updateById(fixedAsset));
    }

    @Operation(summary = "删除固定资产")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        return R.ok(fixedAssetService.removeById(id));
    }
}